package com.ca.ridersclubapplication.view.card;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.bumptech.glide.Glide;
import com.ca.ridersclubapplication.ImageActivity;
import com.ca.ridersclubapplication.R;
import com.ca.ridersclubapplication.util.ImageCacheUtils;

import java.util.ArrayList;
import java.util.List;

public class PaperContent extends ConstraintLayout {

    private String label;
    private Drawable labelBg;

    private Activity activity;

    private List<String> imageUrlList = new ArrayList<>();
    private final List<TableRow> rowList = new ArrayList<>();

    public TextView labelView;
    public TextView contentView;
    public TableLayout imageTL;

    public PaperContent(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public PaperContent(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public PaperContent(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        View view = LayoutInflater.from(context).inflate(R.layout.paper_content_item_layout, this);

        labelView = view.findViewById(R.id.tv_label);
        contentView = view.findViewById(R.id.tv_content);
        imageTL = view.findViewById(R.id.tl_image);

        if (attrs != null) {

            TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PaperContent, 0, 0);

            label = array.getString(R.styleable.PaperContent_pci_label);
            labelBg = array.getDrawable(R.styleable.PaperContent_pci_bg);

            array.recycle();
        }

        setLabel(label);
        setLabelBg(labelBg);
    }


    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label == null ? "" : label;
        labelView.setText(this.label);
    }

    public Drawable getLabelBg() {
        return labelBg;
    }

    public void setLabelBg(Drawable labelBg) {
        this.labelBg = labelBg;
        labelView.setBackground(labelBg);
    }

    public List<String> getImageUrlList() {
        return imageUrlList;
    }

    public void setImageUrlList(List<String> imageUrlList) {
        if (imageUrlList == null) {
            imageUrlList = new ArrayList<>();
        }

        //  清空原来的图片
        imageTL.removeAllViews();

        //  设置图片
        for (String path : imageUrlList) {
            addImage(path);
        }

        //  点击事件
        String[] array = new String[imageUrlList.size()];
        for (int i = 0; i < imageUrlList.size(); i++) {
            array[i] = imageUrlList.get(i);
        }
        for (int i = 0; i < rowList.size(); i++) {
            TableRow row = rowList.get(i);
            int count = row.getChildCount();
            for (int j = 0; j < count; j++) {
                int index = i * 3 + j;
                View view = row.getChildAt(j);
                view.setOnClickListener(v -> {
                    if (activity != null) {
                        Intent intent = new Intent(activity, ImageActivity.class);
                        intent.putExtra("index", index);
                        intent.putExtra("imageUrlArray", array);
                        activity.startActivity(intent);
                    }
                });
            }
        }
    }

    /**
     * 添加图片
     */
    public void addImage(String path) {

        //  判断是否需要添加新的一行
        if (imageUrlList.size() % 3 == 0) {
            //  创建新的一行
            TableRow row = new TableRow(getContext());
            //  初始化该行
            initRow(row);
            //  在布局中添加该行
            imageTL.addView(row);
            //  在列表中添加改行
            rowList.add(row);
        }

        TableRow row = rowList.get(rowList.size() - 1);

        //  创建新的一个图片视图
        ImageView imageView = new ImageView(getContext());
        //  显示图片
        ImageCacheUtils.loadImage(getContext(), path, imageView);
        //  设置该图片视图布局参数
        initEitLayoutParams(imageView);
        //  在布局中添加该视图
        row.addView(imageView);

        //  添加图片地址
        imageUrlList.add(path);
    }

    /**
     * 初始化行
     */
    private void initRow(TableRow row) {
        //  设置布局参数
        TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
        row.setLayoutParams(rowParams);
    }

    private void initEitLayoutParams(ImageView imageView) {
        //  保持图片不变形
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        //  设置图片比重、外边距
        TableRow.LayoutParams params = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1);
        params.setMargins(8, 8, 8, 8);
        imageView.setLayoutParams(params);
        //  设置高度和宽度一致
        imageView.post(() -> {
            ViewGroup.LayoutParams elp = imageView.getLayoutParams();
            elp.height = imageView.getWidth();
            imageView.setLayoutParams(elp);
        });
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }
}
